/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */

#include <QWidget>

#include "tabwidget.h"
#include "singletab.h"
#include "tabzone.h"

tabWidget::tabWidget(QWidget *parent = 0)
{
    setMovable(true);
    setTabsClosable(true);
}

tabWidget::tabWidget(GProgressBar *finestra,QTAstbar *x)
{
    progressBar=finestra;
    qsb=x;
    setMovable(true);
    setTabsClosable(true);
    connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(chiuditab(int)));
    this->manager = new QNetworkAccessManager;
//    singleTab *stab=new singleTab(this->manager);
    singleTab *stab=new singleTab(this,this->manager); // REALLY NEEDED? TO BE CHECKED BY PERTHECT [!!!!]
}

void tabWidget::createTabForURL(QUrl url, QIcon icon, QString label)
{
//    tabZone *tzone = new tabZone(this); // I NEED DA "THIS" (IE DA PARENT) FOR TABTIP UPDATE
    tabZone *tzone = new tabZone(this,manager); // I NEED DA "THIS" (IE DA PARENT) FOR TABTIP UPDATE
    tzone->disegna(url);
    addTab(tzone, icon, label);
    setTabToolTip(count() - 1, url.toString());
    setCurrentIndex(count() - 1);
    qsb->setViewName(url.toString());
}

tabZone *tabWidget::webView(int index) const
{
    QWidget *widget = this->widget(index);
    if (tabZone *mywebView = qobject_cast<tabZone*>(widget)) {
        return mywebView;
    }
    return 0;
}

tabZone *tabWidget::currentWebView() const
{
    return webView(currentIndex());
}

bool tabWidget::noTabsOn()
{
    return count() < 1;
}

int tabWidget::findMatchingTab(QUrl url)
{
    for (int i = 0; i < count(); ++i) {
        QWidget *widget = this->widget(i);
        if (tabZone *tab = qobject_cast<tabZone*>(widget)){
            if(tab->cometichiami() == url)
                return i;
         }
    }
    return count();
}

// aggiungitab ONLY IF NOT ON ALREADY
void tabWidget::aggiungitab(QUrl url,QIcon icon,QString label)
{
// NO TABS ON
    if (noTabsOn()) {
        createTabForURL(url, icon, label);
    } else {
// PARSE ALL TABS FOR MATCH
        int tabIndex = findMatchingTab(url);
        if (tabIndex == count()) {
            createTabForURL(url, icon, label);
        } else {
            setCurrentIndex(tabIndex);
        }
    }
}

void tabWidget::aggiornatab(int index)
{
    //if(index = -8888) // PERTHECT
    index=currentIndex();
    QWidget *widget = this->widget(index);
    if (tabZone *tab = qobject_cast<tabZone*>(widget)){
        tab->view->reload();
        qsb->setViewName((tab->view->url()).toString());
    }
}

void tabWidget::pageback(int index)
{
    //if(index = -8888) // PERTHECT
    index=currentIndex();
    QWidget *widget = this->widget(index);
    if (tabZone *tab = qobject_cast<tabZone*>(widget)){
        tab->view->back();
        qsb->setViewName((tab->view->url()).toString());
    }
}

void tabWidget::pageforward(int index)
{
    //if(index = -8888) // PERTHECT
    index=currentIndex();
    QWidget *widget = this->widget(index);
    if (tabZone *tab = qobject_cast<tabZone*>(widget)){
        tab->view->forward();
        qsb->setViewName((tab->view->url()).toString());
    }
}

void tabWidget::stopload(int index)
{
    //if(index = -8888) // PERTHECT
    index=currentIndex();
    QWidget *widget = this->widget(index);
    if (tabZone *tab = qobject_cast<tabZone*>(widget)){
        tab->view->stop();
//        qsb->setViewName((tab->view->url()).toString());
    }
}

void tabWidget::aggiornatabtip(QUrl url)
{
    setTabToolTip(currentIndex(),url.toString());
}

void tabWidget::chiuditab(int tab)
{
   removeTab(tab);
}
